package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.innovation.chengzhangriji.ui.CameraHelper;

import java.io.IOException;
import java.util.List;

/**
 * A basic Camera preview class
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = CameraPreview.class.getSimpleName();

    private Camera mCamera;

    private SurfaceHolder mHolder;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        Camera.Parameters params = camera.getParameters();
        List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        Log.d("widthPixels", "" + dm.widthPixels);
        Log.d("heightPixels", "" + dm.heightPixels);
        Camera.Size sizePreview = CameraHelper.getOptimalPreviewSize(supportedPreviewSizes, dm.heightPixels, dm.widthPixels);
        Log.d("sizePreview width", "" + sizePreview.width);
        Log.d("sizePreview height", "" + sizePreview.height);
        List<Camera.Size> supportedPictureSizes = params.getSupportedPictureSizes();
        // Camera.Size sizePicture = CameraHelper.getOptimalPictureSize(supportedPictureSizes, sizePreview.width * 1.0f / sizePreview.height);
        // Camera.Size sizePicture = CameraHelper.getOptimalPictureSizeSmall(supportedPictureSizes, sizePreview.width * 1.0f / sizePreview.height);
        Camera.Size sizePicture = CameraHelper.getOptimalPreviewSize(supportedPictureSizes, 480, 320);

        Log.d("sizePicture width", "" + sizePicture.width);
        Log.d("sizePicture height", "" + sizePicture.height);
        params.setPreviewSize(sizePreview.width, sizePreview.height);
        params.setPictureSize(sizePicture.width, sizePicture.height);
        params.setPictureFormat(PixelFormat.JPEG);
        mCamera.setParameters(params);
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            if(getWidth() > getHeight()) {
                mCamera.setDisplayOrientation(0);
            } else {
                mCamera.setDisplayOrientation(90);
            }
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            if(w > h) {
                mCamera.setDisplayOrientation(0);
            } else {
                mCamera.setDisplayOrientation(90);
            }
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

}